package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.*;
import com.itheima.health.pojo.*;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServicelmpl implements SetmealService {

    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckGroupItemDao checkGroupItemDao;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealCheckGroupDao setemalCheckGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;



    @Transactional
    @Override
    public void add(List<Integer> checkgroupIds, Setmeal setmeal) {
       log.info("[套餐-添加]data：{}", setmeal);
       setmealDao.insert(setmeal);

        List<SetmealCheckGroup> setmealCheckGroups = new ArrayList<>();
        List<CheckGroup> checkGroups = new ArrayList<>();

        for (Integer checkgroupId : checkgroupIds) {
            CheckGroup checkGroup = checkGroupDao.selectById(checkgroupId);
            checkGroups.add(checkGroup);
        }

        for (CheckGroup checkGroup : checkGroups) {
            SetmealCheckGroup setmealCheckGroup = new SetmealCheckGroup();
            setmealCheckGroup.setCheckgroupId(checkGroup.getId());
            setmealCheckGroup.setSetmealId(setmeal.getId());
            setmealCheckGroup.setCode(checkGroup.getCode());
            setmealCheckGroup.setName(checkGroup.getName());
            setmealCheckGroup.setRemark(checkGroup.getRemark());

//            setmealCheckGroups.add(setmealCheckGroup);

            setemalCheckGroupDao.insert(setmealCheckGroup);
        }
    }


    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[套餐-分页查询]data：{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page = setmealDao.selectByConditon(queryPageBean.getQueryString());
        //封装分页数据t_checkgroup
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        log.info("[套餐-查询所有]~");
      return setmealDao.selectAll();
    }

    /**
     *体检预约(套餐展示)
     * @return
     */
    public List<Setmeal> getSetmeal(){
        List<Setmeal> setmeals = setmealDao.getSetmeal();
        return setmeals;
    }

    /**
     * 套餐详情
     * 根据id查询检查组
     * @param id
     * @return
     */
    @Override
    public List<CheckGroup> findGroupsBySetmealId(Integer id) {
        //根据套餐id获取检查组 关联套餐检查组表
        List<SetmealCheckGroup> setmealCheckGroups = setemalCheckGroupDao.findGroupsBySetmealId(id);

        //创建一个检查组集合
        List<CheckGroup> checkGroups = new ArrayList<>();
        //遍历套餐检查组
        for (SetmealCheckGroup setmealCheckGroup : setmealCheckGroups) {
            //每遍历一次套餐检查组就提取一次检查组
            CheckGroup checkGroup = checkGroupDao.selectById(setmealCheckGroup.getCheckgroupId());

            //创建一个检查项集合
            List<CheckItem> checkItems = new ArrayList<>();
            //根据检查组id 获取 检查组项
            List<CheckGroupItem> checkGroupItems = checkGroupItemDao.getByCheckgroupId(checkGroup.getId());
            //遍历 检查组项
            for (CheckGroupItem checkGroupItem : checkGroupItems) {
                //每遍历一次检查组项就提取一次检查项
                CheckItem checkItem = checkItemDao.selectById(checkGroupItem.getCheckitemId());
                //将新的检查项封装到检查项集合中
                checkItems.add(checkItem);
            }
            //将检查项赋值检查组
            checkGroup.setCheckItems(checkItems);
            //将检查组 存入检查组集合
            checkGroups.add(checkGroup);
        }
        return checkGroups ;
    }

    /**
     * 套餐详情
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal selectById(Integer id) {
        Setmeal setmeal = setmealDao.selectById(id);
        return setmeal;
    }
}
